//    Copyright (C) Kherty.  All rights reserved.
using System.Diagnostics;
using System.Windows.Input;
using OpenLS.Spreadsheet.UI.Internals;
using OpenLS.Spreadsheet.UI.Internals.Resources;

namespace OpenLS.Spreadsheet.UI
{
    public static class BorderCommands
    {
        private static readonly RoutedUICommand[] _internalCommands = new RoutedUICommand[(int) CommandId.Last];


        public static RoutedCommand ClearBorders
        {
            get { return EnsureCommand(CommandId.ClearBorders); }
        }

        //  static private RoutedCommand setTopBorder = new RoutedUICommand("Top border", "SetTopBorder", typeof(BorderCommands));
        public static RoutedCommand SetTopBorder
        {
            get { return EnsureCommand(CommandId.SetTopBorder); }
        }

        //  static private RoutedCommand setBottomBorder = new RoutedUICommand("Bottom border", "SetBottomBorder", typeof(BorderCommands));
        public static RoutedCommand SetBottomBorder
        {
            get { return EnsureCommand(CommandId.SetBottomBorder); }
        }

        //  static private RoutedCommand setLeftBorder = new RoutedUICommand("Left border", "SetLeftBorder", typeof(BorderCommands));
        public static RoutedCommand SetLeftBorder
        {
            get { return EnsureCommand(CommandId.SetLeftBorder); }
        }

        //  static private RoutedCommand setRightBorder = new RoutedUICommand("Right border", "SetRightBorder", typeof(BorderCommands));
        public static RoutedCommand SetRightBorder
        {
            get { return EnsureCommand(CommandId.SetRightBorder); }
        }

        //  static private RoutedCommand setTopAndBottomBorders = new RoutedUICommand("Top and bottom borders", "TopAndBottomBorders", typeof(BorderCommands));
        public static RoutedCommand SetTopAndBottomBorders
        {
            get { return EnsureCommand(CommandId.SetTopAndBottomBorders); }
        }


        //  static private RoutedCommand setAllBorders = new RoutedUICommand("All borders", "SetAllBorder", typeof(BorderCommands));
        public static RoutedCommand SetAllBorders
        {
            get { return EnsureCommand(CommandId.SetAllBorders); }
        }

        //  static private RoutedCommand setOutsideBorders = new RoutedUICommand("Outside borders", "SetOutsideBorder", typeof(BorderCommands));
        public static RoutedCommand SetOutsideBorders
        {
            get { return EnsureCommand(CommandId.SetOutsideBorders); }
        }

        private static RoutedUICommand EnsureCommand(CommandId commandId)
        {
            return EnsureCommand(commandId, commandId.ToString());
        }

        private static RoutedUICommand EnsureCommand(CommandId idCommand, string propertyName)
        {
            lock (_internalCommands.SyncRoot)
            {
                if (_internalCommands[(int) idCommand] == null)
                {
                    string text = GetUIText(idCommand);
                    if (text == null)
                        text = "!!Missing" + idCommand;
                    RoutedUICommand newCommand = new RoutedUICommand(text, propertyName, typeof (ClipboardCommands));
                    _internalCommands[(int) idCommand] = newCommand;
                }
            }
            return _internalCommands[(int) idCommand];
        }

        private static string GetUIText(CommandId idCommand)
        {
            switch (idCommand)
            {
                case CommandId.SetBottomBorder:
                    return UIStringTable.SetBottomBorder;
                case CommandId.SetTopBorder:
                    return UIStringTable.SetTopBorder;
                case CommandId.SetLeftBorder:
                    return UIStringTable.SetLeftBorder;
                case CommandId.SetRightBorder:
                    return UIStringTable.SetRightBorder;
                case CommandId.ClearBorders:
                    return UIStringTable.ClearBorders;
                case CommandId.SetAllBorders:
                    return UIStringTable.SetAllBorders;
                case CommandId.SetOutsideBorders:
                    return UIStringTable.SetOutsideBorders;
                case CommandId.SetTopAndBottomBorders:
                    return UIStringTable.SetTopAndBottomBorders;

                default:
                    Debug.WriteLine("case " + idCommand + " : return UIStringTable." + idCommand + ";");
                    return idCommand.ToString();
            }
        }

        #region Nested type: CommandId

        private enum CommandId
        {
            ClearBorders,
            SetTopBorder,
            SetBottomBorder,
            SetLeftBorder,
            SetRightBorder,
            SetTopAndBottomBorders,
            SetAllBorders,
            SetOutsideBorders,
            Last
        }

        #endregion
    }
}